﻿var Sys_UserRolesList;
var Sys_UserRolesStore;
// 功能模块grid展示
Sys_UserRolesList = function() {
    var url = "/Sys_UserRoles/List";
    var sm = Ext.create('Ext.selection.CheckboxModel');

    //grid 填充
    Ext.define('Sys_UserRolesGridModel', {
        extend: 'Ext.data.Model',
        fields: [
            { name: "R_UserID", type:"string"},
            { name: "R_RoleID", type:"string"}
            ]
    });

    //grid获取 json数组
    Sys_UserRolesStore = Ext.create('Ext.data.Store', {
        pageSize: CMN.pageSize,
        model: 'Sys_UserRolesGridModel',
        buffered: true,
        proxy: {
            actionMethods: 'post',
            type: 'ajax',
            url: url,
            reader: {
                type: 'json',
                root: 'root',
                totalProperty: 'totalPorperty'
            }
        },
        sorters: [{
            property: 'ModuleID',
            direction: 'DESC'
}]
        });

        //分页设置
        var pagingBar = Ext.create('Ext.PagingToolbar', {
            displayInfo: true, emptyMsg: "没有数据显示", displayMsg: "显示从{0}条数据到{1}条数据，共{2}条数据",
            store: Sys_UserRolesStore,
            pageSize: CMN.pageSize,
            //显示右下角信息
            prevText: "上一页",
            nextText: "下一页",
            refreshText: "刷新",
            lastText: "最后页",
            firstText: "第一页",
            beforePageText: "当前页",
            afterPageText: "共{0}页"
        });

        //删除选中行信息
        DeleteSys_UserRolesFn = function() {
            var row = Ext.getCmp("Sys_UserRolesPanel").getSelectionModel().getSelection();
            if (row.length == 0) {
                Ext.Msg.alert("提示信息", "请您至少选择一个!");
            }
            else if (row.length > 1) {
                Ext.Msg.alert("提示信息", "对不起只能选择一个!");
            }
            else if (row.length == 1) {
                Ext.Msg.confirm("提 示!", "您确定要删除" + row[0].data.ModuleID + "吗?", function(btn) {
                    if (btn == "yes") {
                        Ext.Ajax.request({
                            url: "/Sys_UserRoles/Delete/" + row[0].data.ModuleID,
                            method: "POST",
                            success: function(d) {
                                //成功后
                                var action = Ext.JSON.decode(d.responseText);
                                //alert(action.success);
                                var flag = action.success;
                                if (flag == "true") {
                                    Sys_UserRolesStore.loadPage(1);
                                    Ext.MessageBox.alert("恭喜", "删除成功!");
                                }
                                else {
                                    var message = action.result.message;
                                    if (message) {
                                        Ext.MessageBox.alert("删除失败!", message);
                                    }
                                    else {
                                        Ext.MessageBox.alert("提示!", "删除失败!");
                                    }
                                }
                            },
                            failure: function(form, action) {
                                Ext.MessageBox.alert("提示!", "修改失败!");
                            }
                        })
                    }
                    else {

                    }
                })
            }
        }


        //修改选中行信息
        EditSys_UserRolesFn = function() {
            var row = Ext.getCmp("Sys_UserRolesPanel").getSelectionModel().getSelection();
            if (row.length == 0) {
                Ext.Msg.alert("提示信息", "您没有选中任何行!");
            }
            else if (row.length > 1) {
                Ext.Msg.alert("提示信息", "对不起只能选择一个!");
            } else if (row.length == 1) {
                EditSys_UserRoles(row[0].data.ModuleID);
            }

        }

        //新增信息
        SaveSys_UserRolesFn = function() {
            InsertSys_UserRoles();
        }

        //grid配置
        var grid = Ext.create('Ext.grid.Panel', {
            id: "Sys_UserRolesPanel",
            renderTo: "Sys_UserRolesGrid",
            layout: "fit",
            alias: 'widget.methodPanel',
            columnLines: true,
            loadMask: true,
            frame: true,
            border: false,
            selModel: sm,   //选择框
            height: 400,
            autoScroll: true,
            store: Sys_UserRolesStore,
            ds: Sys_UserRolesStore,
            sm: sm,
            columnLines: true,
            //cm: cm2,
            viewConfig: {
                stripeRows: true,
                trackOver: true,
                emptyText: '<div style="color:#999;margin:5px;">当前没有记录显示<div>'
            },
            stripeRows: true,
            bbar: pagingBar,
            tbar: [            		 //工具条设置
            		 {
            		 text: "新增",
            		 tooltip: "新增",
            		 iconCls: "addicon",
            		 handler: SaveSys_UserRolesFn
           }, "", "-", "", {
               text: "编辑",
               tooltip: "编辑",
               iconCls: "editicon",
               handler: EditSys_UserRolesFn
           }, "", "-", "", {
               text: "删除",
               tooltip: "删除",
               iconCls: "deleteicon",
               handler: DeleteSys_UserRolesFn
           }, "-"],
            columns: [
               Ext.create('Ext.grid.RowNumberer'),
                { header: "用户ID与sys_User表中UserID相关", dataIndex: "R_UserID", width: 80, sortable: true },
                { header: "用户所属角色ID与Sys_Roles关联", dataIndex: "R_RoleID", width: 80, sortable: true }
                      ]

        });
        Sys_UserRolesStore.loadPage(1);
       
    }
    Sys_UserRolesList();


    Ext.onReady(function() {

        Ext.Ajax.request({
        url: "/Sys_Roles/List",
            params: {},
            success: function(response, config) {
                json = Ext.JSON.decode(response.responseText);

                var Sys_RolePermissionList;
                var Sys_RolePermissionStore;
                // 功能模块grid展示
                Sys_RolePermissionList = function() {
                    var url = "/Sys_RolePermission/List";
                    //var sm = Ext.create('Ext.selection.RowModel');

                    //grid 填充
                    Ext.define('Sys_RolePermissionGridModel', {
                        extend: 'Ext.data.Model',
                        fields: json.fieldsNames
                    });

                    var groupingFeature = Ext.create('Ext.grid.feature.Grouping', {
                        groupHeaderTpl: 'text1: {name} ({rows.length} Item{[values.rows.length > 1 ? "s" : ""]})'
                    });

                    //grid获取 json数组
                    Sys_RolePermissionStore = Ext.create('Ext.data.Store', {
                        pageSize: 10,
                        model: 'Sys_RolePermissionGridModel',
                        buffered: true,
                        groupField: 'text1',
                        data: json.data,
                        proxy: {
                            //actionMethods: 'post',
                            type: 'memory'
                            //url: url,
                            //                        reader: {
                            //                            type: 'json',
                            //                            root: 'root',
                            //                            totalProperty: 'totalPorperty'
                            //                        }
                        },
                        sorters: [{
                            property: 'number',
                            direction: 'DESC'
}]
                        });
                        SaveSys_RolePermissionFn = function() {
                            var rowcount = grid.getStore().getCount();
                            console.log(rowcount);
                            for (i = 0; i < rowcount; i++) {
                                console.log(i);
                                var abc = grid.getView().getNode(i);
                                var childs = abc.children[3].children[0];

                                console.log(childs.childNodes.length);
                                //console.log(childs.innerHTML);
                                for (var j = 0; j < childs.children.length; j++) {
                                    console.log(childs.children[i].value + childs.children[i].checked);
                                }


                                //var cell = grid.getView().getCell(i, 3);
                                //console.log(cell.outerText);

                            }


                        }
                        //grid配置
                        var grid = Ext.create('Ext.grid.Panel', {
                            id: "Sys_RolePermissionPanel",
                            renderTo: "Sys_RolePermissionGrid",
                            layout: "fit",
                            alias: 'widget.methodPanel',
                            columnLines: true,
                            loadMask: true,
                            frame: true,
                            border: false,
                            //selModel: sm,   //选择框
                            height: 400,
                            autoScroll: true,
                            store: Sys_RolePermissionStore,
                            ds: Sys_RolePermissionStore,
                            //sm: sm,
                            columnLines: true,
                            plugins: [rowEditing],
                            features: [groupingFeature],
                            //cm: sm,
                            viewConfig: {
                                stripeRows: true,
                                trackOver: true,
                                emptyText: '<div style="color:#999;margin:5px;">当前没有记录显示<div>'
                            },
                            stripeRows: true,
                            tbar: [            		 //工具条设置
            		 {
            		 text: "新增",
            		 tooltip: "新增",
            		 iconCls: "addicon",
            		 handler: SaveSys_RolePermissionFn
}],
                            columns: json.columModle

                        });
                        //Sys_RolePermissionStore.loadPage(1);
                       
                    }
                    Sys_RolePermissionList();

                },
                failure: function() {
                    alert("sdf");

                }
            });
        });
    
